home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-28 | 13.3 KB | 443 lines | [TEXT/MPS ] |
- /*
- File: ObjectHe.h
-
- Contains: ObjectHeap class interface
-
- Written by: Michael Burbidge
-
- Copyright: © 1993 - 1994 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <4> 8/17/94 jpa Added warning about incompatibility.
- <3> 8/8/94 jpa OD_DEBUG --> ODDebug
- <2> 6/10/94 MB Make it build
- <1> 6/9/94 MB first checked in
- <2> 5/9/94 MB #1162181: Changes necessary to install MMM.
- <1> 4/29/94 MB first checked in
- To Do:
- */
-
- $$$$$ This class is not being used as of 8/11/94. It is not quite up to date with
- the changes I am making to the block headers in BestFitH; if we want to use
- this class again we'll need to update PrivChunkyBlock to make it compatible
- with PrivBestFitBlock. --jpa
-
-
- #ifndef _OBJECTHE_
- #define _OBJECTHE_
-
- #ifndef _PLATFMEM_
- #include "PlatfMem.h"
- #endif
-
- #ifndef _MEMORYHE_
- #include "MemoryHe.h"
- #endif
-
- #ifndef _BESTFITH_
- #include "BestFitH.h"
- #endif
-
-
- //========================================================================================
- // Forward class declarations
- //========================================================================================
-
- class PrivChunk;
- class PrivChunkyBlock;
- class PrivChunkyBlockStack;
-
-
- //========================================================================================
- // CLASS PrivChunkyBlock
- //========================================================================================
-
- #ifdef BUILD_WIN
- // Bytes are in reverse order in a word.
-
- const unsigned short ChunkyBlock_kSizeIndexMask = 0x00F0;
- const unsigned short ChunkyBlock_kSizeIndexShift = 4;
-
- const unsigned short ChunkyBlock_kBlockIndexMask = 0x000F;
- const unsigned short ChunkyBlock_kBlockIndexShift = 0;
-
- const unsigned short ChunkyBlock_kBlockTypeMask = 0xF000;
- const unsigned short ChunkyBlock_kBlockTypeShift = 12;
-
- const unsigned short ChunkyBlock_kMagicNumberMask = 0x0F00;
- const unsigned short ChunkyBlock_kMagicNumberShift = 8;
- #else
- const unsigned short ChunkyBlock_kSizeIndexMask = 0xF000;
- const unsigned short ChunkyBlock_kSizeIndexShift = 12;
-
- const unsigned short ChunkyBlock_kBlockIndexMask = 0x0F00;
- const unsigned short ChunkyBlock_kBlockIndexShift = 8;
-
- const unsigned short ChunkyBlock_kBlockTypeMask = 0x00F0;
- const unsigned short ChunkyBlock_kBlockTypeShift = 4;
-
- const unsigned short ChunkyBlock_kMagicNumberMask = 0x000F;
- const unsigned short ChunkyBlock_kMagicNumberShift = 0;
- #endif
-
- class PrivChunkyBlock
- {
- public:
- enum
- {
- kBusyOverhead = sizeof(unsigned short),
- kBlockTypeId = PrivBestFitBlock::kBlockTypeId + 1,
- kMagicNumber = 0xA
- };
-
- void *operator new(SIZE_T, void *ptr);
- void *operator new(SIZE_T);
- void operator delete(void *) { };
-
- PrivChunkyBlock();
- PrivChunkyBlock(unsigned int sizeIndex, unsigned int blockIndex);
- PrivChunkyBlock(const PrivChunkyBlock& blk);
- PrivChunkyBlock& operator=(const PrivChunkyBlock& blk);
-
- unsigned short GetSizeIndex() const;
- void SetSizeIndex(unsigned short index);
-
- unsigned short GetBlockIndex() const;
- void SetBlockIndex(unsigned short index);
-
- unsigned short GetBlockType() const;
- void SetBlockType(unsigned short type);
-
- unsigned short GetMagicNumber() const;
- void SetMagicNumber(unsigned short magic);
-
- PrivChunk* GetChunk(ODBlockSize blkSize);
-
- PrivChunkyBlock* GetNext();
- void SetNext(PrivChunkyBlock* blk);
-
- Boolean IsBusy(ODBlockSize blockSize);
- void SetBusy(ODBlockSize blockSize, Boolean busy);
-
- private:
- // Fields present in both free and busy blocks. Several bit fields are stored in
- // the following fields. They are accessed using get and set methods.
-
- unsigned short fBits;
-
- // Fields present in only free blocks.
-
- PrivChunkyBlock* fNext;
- };
-
-
- //========================================================================================
- // CLASS PrivChunkyBlockStack
- //========================================================================================
-
- class PrivChunkyBlockStack
- {
- public:
- PrivChunkyBlockStack();
- PrivChunkyBlockStack(const PrivChunkyBlockStack& blk);
- ~PrivChunkyBlockStack();
- PrivChunkyBlockStack& operator=(const PrivChunkyBlockStack& blk);
-
- void *operator new(size_t size);
- void operator delete(void *);
-
- PrivChunkyBlock* Pop();
- void Push(PrivChunkyBlock* blk);
- void RemoveRange(void *begAddr, void *endAddr);
- PrivChunkyBlock* Top();
-
- private:
- PrivChunkyBlock fHead;
- };
-
-
- //========================================================================================
- // CLASS PrivChunk
- //========================================================================================
-
- struct ODPrivChunkHeader
- {
- unsigned short fBlockBusyBits;
- };
-
- class PrivChunk
- {
- public:
- PrivChunk(short blocksPerChunk,
- unsigned int sizeIndex,
- ODBlockSize blockSize);
-
- void *operator new(SIZE_T, void* ptr);
- void *operator new(SIZE_T);
- void operator delete(void *) { };
-
- PrivChunkyBlock* GetBlock(unsigned int blkIndex, ODBlockSize blkSize);
- unsigned int GetSizeIndex();
-
- Boolean IsBlockBusy(unsigned int whichBlock);
- Boolean IsBusy();
-
- void SetBlockBusy(unsigned int whichBlock, Boolean busy);
-
- private:
- ODPrivChunkHeader fHeader;
-
- PrivChunk(const PrivChunk& blk);
- PrivChunk& operator=(const PrivChunk& blk);
- // This class shouldn't be copied.
- };
-
-
- //========================================================================================
- // CLASS ObjectHeap
- //========================================================================================
-
- extern const ODBlockSize ObjectHeap_kDefaultBlockSizes[];
-
- class ObjectHeap : public BestFitHeap
- {
- private:
-
- public:
- enum
- {
- kMaxNumberOfBlockSizes = 16,
- kDefaultBlocksPerChunk = 4,
- kDefaultInitialSize = 10240,
- kDefaultIncrementSize = 4096
- };
-
- ObjectHeap(unsigned long initialSize,
- unsigned long incrementSize = 0,
- unsigned long hugeBlockSize = 0, // 0 means incrementSize/2
- Boolean fromSysMemory = false,
- short blocksPerChunk = kDefaultBlocksPerChunk);
-
- ObjectHeap(const ODBlockSize* blockSizes,
- unsigned long initialSize = kDefaultInitialSize,
- unsigned long incrementSize = kDefaultIncrementSize,
- unsigned long hugeBlockSize = 0, // 0 means incrementSize/2
- Boolean fromSysMemory = false,
- short blocksPerChunk = kDefaultBlocksPerChunk);
-
- void IObjectHeap();
-
- virtual ~ObjectHeap();
-
- protected:
- virtual void* DoAllocate(ODBlockSize size, ODBlockSize& allocatedSize);
- virtual ODBlockSize DoBlockSize(const void* block) const;
- virtual void DoFree(void*);
- virtual void DoReset();
-
- void* AllocateBlock(unsigned int sizeIndex);
- void CreateNewChunk(unsigned int sizeIndex);
- void FreeBlock(PrivChunkyBlock* blk);
- unsigned int SizeIndex(ODBlockSize size);
-
- #if ODDebug
- virtual void CompilerCheck();
- virtual Boolean DoIsValidBlock(void* blk) const;
- #endif
-
- private:
-
- short fNumberOfBlockSizes;
- const ODBlockSize* fBlockSizes;
- PrivChunkyBlockStack fFreeLists[kMaxNumberOfBlockSizes];
- short fBlocksPerChunk;
-
- ObjectHeap(const ObjectHeap& blk);
- ObjectHeap& operator=(const ObjectHeap& blk);
- // This class shouldn't be copied.
- };
-
-
- //========================================================================================
- // CLASS PrivChunkyBlock
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::operator new
- //----------------------------------------------------------------------------------------
-
- inline void *PrivChunkyBlock::operator new(SIZE_T, void *ptr)
- {
- return ptr;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::operator new
- //----------------------------------------------------------------------------------------
-
- inline void *PrivChunkyBlock::operator new(SIZE_T)
- {
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::PrivChunkyBlock
- //----------------------------------------------------------------------------------------
-
- inline PrivChunkyBlock::PrivChunkyBlock(const PrivChunkyBlock& blk) :
- fBits(blk.fBits),
- fNext(blk.fNext)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::operator=
- //----------------------------------------------------------------------------------------
-
- inline PrivChunkyBlock& PrivChunkyBlock::operator=(const PrivChunkyBlock& blk)
- {
- fBits = blk.fBits;
- fNext = blk.fNext;
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::GetSizeIndex
- //----------------------------------------------------------------------------------------
-
- inline unsigned short PrivChunkyBlock::GetSizeIndex() const
- {
- return (fBits & ChunkyBlock_kSizeIndexMask) >> ChunkyBlock_kSizeIndexShift;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::SetSizeIndex
- //----------------------------------------------------------------------------------------
-
- inline void PrivChunkyBlock::SetSizeIndex(unsigned short index)
- {
- fBits &= ~ChunkyBlock_kSizeIndexMask;
- fBits |= (index << ChunkyBlock_kSizeIndexShift) & ChunkyBlock_kSizeIndexMask;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::GetBlockIndex
- //----------------------------------------------------------------------------------------
-
- inline unsigned short PrivChunkyBlock::GetBlockIndex() const
- {
- return (fBits & ChunkyBlock_kBlockIndexMask) >> ChunkyBlock_kBlockIndexShift;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::SetBlockIndex
- //----------------------------------------------------------------------------------------
-
- inline void PrivChunkyBlock::SetBlockIndex(unsigned short index)
- {
- fBits &= ~ChunkyBlock_kBlockIndexMask;
- fBits |= (index << ChunkyBlock_kBlockIndexShift) & ChunkyBlock_kBlockIndexMask;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::GetBlockType
- //----------------------------------------------------------------------------------------
-
- inline unsigned short PrivChunkyBlock::GetBlockType() const
- {
- return (fBits & ChunkyBlock_kBlockTypeMask) >> ChunkyBlock_kBlockTypeShift;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::SetBlockType
- //----------------------------------------------------------------------------------------
-
- inline void PrivChunkyBlock::SetBlockType(unsigned short type)
- {
- fBits &= ~ChunkyBlock_kBlockTypeMask;
- fBits |= (type << ChunkyBlock_kBlockTypeShift) & ChunkyBlock_kBlockTypeMask;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::GetMagicNumber
- //----------------------------------------------------------------------------------------
-
- inline unsigned short PrivChunkyBlock::GetMagicNumber() const
- {
- return (fBits & ChunkyBlock_kMagicNumberMask) >> ChunkyBlock_kMagicNumberShift;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::SetMagicNumber
- //----------------------------------------------------------------------------------------
-
- inline void PrivChunkyBlock::SetMagicNumber(unsigned short magic)
- {
- fBits &= ~ChunkyBlock_kMagicNumberMask;
- fBits |= (magic << ChunkyBlock_kMagicNumberShift) & ChunkyBlock_kMagicNumberMask;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::GetNext
- //----------------------------------------------------------------------------------------
-
- inline PrivChunkyBlock* PrivChunkyBlock::GetNext()
- {
- return fNext;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunkyBlock::SetNext
- //----------------------------------------------------------------------------------------
-
- inline void PrivChunkyBlock::SetNext(PrivChunkyBlock* blk)
- {
- fNext = blk;
- }
-
-
- //========================================================================================
- // CLASS PrivChunk
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // PrivChunk::operator new
- //----------------------------------------------------------------------------------------
-
- inline void* PrivChunk::operator new(SIZE_T, void *ptr)
- {
- return ptr;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunk::operator new
- //----------------------------------------------------------------------------------------
-
- inline void* PrivChunk::operator new(SIZE_T)
- {
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunk::GetSizeIndex
- //----------------------------------------------------------------------------------------
-
- inline unsigned int PrivChunk::GetSizeIndex()
- {
- PrivChunkyBlock *block
- = (PrivChunkyBlock *) ((ODBytePtr) this + sizeof(ODPrivChunkHeader));
- return block->GetSizeIndex();
- }
-
- //----------------------------------------------------------------------------------------
- // PrivChunk::IsBusy
- //----------------------------------------------------------------------------------------
-
- inline Boolean PrivChunk::IsBusy()
- {
- return fHeader.fBlockBusyBits != 0;
- }
-
- #endif
-